﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddMeeting : System.Web.UI.Page
    {

        MemberAL memberAL = new MemberAL();
        AdminAL adminAL = new AdminAL();
        public Guid SelectedMember
        {
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }




        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            List<MemberDTO> memberDTO = memberAL.GetAllMembers();
            List<MeetingDTO> meetingDTO = memberAL.GetAllMeetings();
            ddlMinutesMeetingName.DataSource = meetingDTO;
            ddlMinutesMeetingName.DataTextField = "Title";
            ddlMinutesMeetingName.DataValueField = "MeetingId";
            ddlMinutesMeetingName.DataBind();


            ddlMinutesSumittedBy.DataSource = memberDTO;
            ddlMinutesSumittedBy.DataTextField = "FullName";
            ddlMinutesSumittedBy.DataValueField = "MemberId";
            ddlMinutesSumittedBy.SelectedValue = SelectedMember.ToString();
            ddlMinutesSumittedBy.DataBind();

            ddlMinutesProposedBy.DataSource = memberDTO;
            ddlMinutesProposedBy.DataTextField = "FullName";
            ddlMinutesProposedBy.DataValueField = "MemberId";
            ddlMinutesProposedBy.SelectedValue = SelectedMember.ToString();
            ddlMinutesProposedBy.DataBind();

            ddlMinutesSecondedBy.DataSource = memberDTO;
            ddlMinutesSecondedBy.DataTextField = "FullName";
            ddlMinutesSecondedBy.DataValueField = "MemberId";
            ddlMinutesSecondedBy.SelectedValue = SelectedMember.ToString();
            ddlMinutesSecondedBy.DataBind();





        }


        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void btnMinutesSubmit_Click(object sender, EventArgs e)
        {
            MemberAL membrAL = new MemberAL();
            MeetingDTO meetingDTO = membrAL.GetMeetingById(new Guid(ddlMinutesMeetingName.SelectedValue)).FirstOrDefault();
            String meetingTitle = meetingDTO.Title;

            string minuteTitle = "The " + ddlMinutesType.SelectedValue + " for the " + meetingTitle;
            try
            {

                MinutesDTO minutesDTO = new MinutesDTO
                {
                    MinutesId = Guid.NewGuid(),
                    MeetingId = new Guid(ddlMinutesMeetingName.SelectedValue),
                    MinutesTitle = minuteTitle,
                    Type = ddlMinutesType.SelectedValue,
                    Description = txtMinutesDesc.Text,
                    SubmittedBy = new Guid(ddlMinutesSumittedBy.SelectedValue),
                    ProposedBy = new Guid(ddlMinutesProposedBy.SelectedValue),
                    SecondedBy = new Guid(ddlMinutesSecondedBy.SelectedValue),
                    FilePath = fuMinutesFile.FileName,
                    //ModifiedBy = "Tharsan",
                    ModifiedTime = System.DateTime.Now
                };
                MemberAL memberAL = new MemberAL();
                string result = memberAL.saveMinutes(minutesDTO);
                if (result == "Saved")
                {
                    lblAddMinuteMsg.Text = "The Minutes Details added Successfully";
                    txtMinutesDesc.Text = "";
                    


                }
                else if (result == "Updated")
                {
                    lblAddMinuteMsg.Text = "The Minutes Details Updated Successfully";
                    txtMinutesDesc.Text = "";
                   
                }
                else if (result == "AlreadyExists")
                {
                    lblAddMinuteMsg.Text = "The Minutes Name is already exists for this Meeting";
                    txtMinutesDesc.Text = "";
                }
                else
                {
                    lblAddMinuteMsg.Text = "Your data cannot be added";
                    txtMinutesDesc.Text = "";
                }

                if ((fuMinutesFile.PostedFile != null) && (fuMinutesFile.PostedFile.ContentLength > 0))
                {
                    string fileName = System.IO.Path.GetFileName(fuMinutesFile.PostedFile.FileName);
                    string saveLocation = Server.MapPath("minutes") + "\\" + fileName;
                    try
                    {
                        fuMinutesFile.PostedFile.SaveAs(saveLocation);
                        Response.Write("The File has been uploaded successfully");
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error occured while uploading the files...!!!" + ex);
                    }
                }
                else
                {
                    Response.Write("Please select a file to upload.");

                }
            }
            catch (Exception ee)
            {
                lblAddMinuteMsg.Text = "There is an exception occured while inserting the Minutes details " + ee.ToString();
            }
        }
    }
}
